package com.jinlia.show.modules.wxapp.controller;

import com.alibaba.fastjson.JSON;
import com.jinlia.show.modules.base.wxapp.entity.AccessToken;
import com.jinlia.show.modules.base.model.po.UserPO;
import com.jinlia.show.modules.wxapp.model.form.LoginForm;
import com.jinlia.show.modules.wxapp.service.AccessTokenService;
import com.jinlia.show.modules.wxapp.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private AccessTokenService accessTokenService;

    /**
     * 微信登录
     *
     * @param loginForm
     * @return
     */
    @PostMapping
    public UserPO login(@RequestBody LoginForm loginForm) {
        UserPO userPO = userService.login(loginForm.getCode(),loginForm.getAppId());
        log.info("微信登录 jsonObject={} ", JSON.toJSONString(userPO));
        return userPO;
    }

    /**
     * 获取当前用户-手机号
     *
     * @return
     */
    @GetMapping("/get/phone")
    public UserPO getUserPhoneNumber(@RequestParam("code") String code) {
        UserPO userPO = userService.getUserPhoneNumber(code);
        log.info("获取当前用户-手机号 wxUserInfo={} ", JSON.toJSONString(userPO));
        return userPO;
    }

    @GetMapping("/token")
    public AccessToken getToken() {
        AccessToken accessToken = accessTokenService.getAccessToken();
        return accessToken;
    }
}
